$(function() {
	
	//匹配规则
	sys.selectpicker({
		id 		: 'matchRule', // select的id
		url 	: rootPath + "/dict/listKeyMapEntites", // 请求数据的远程地址
		param 	: { keyType : 'keywordMatchRule' }, // 过滤参数
		defaultValue: $('#matchRule').attr("data-value") || "1",
		valueField 	: 'keyValue',
		textField 	: 'keyIntro',
		
	});
	
	//申请开始时间
	sys.datepicker({id: 'effectiveBeginTime', type:'datetime', maxDate: null});
	if ($('#effectiveBeginTime').attr('data-value')) {
		$('#effectiveBeginTime').val($('#effectiveBeginTime').attr('data-value'))
	} else {
		$('#effectiveBeginTime').val('');
	}
 	//申请结束时间
	sys.datepicker({id: 'effectiveEndTime', type:'datetime', maxDate: null});
	if ($('#effectiveEndTime').attr('data-value')) {
		$('#effectiveEndTime').val($('#effectiveEndTime').attr('data-value'))
	} else {
		$('#effectiveEndTime').val('');
	}
	
	$('#btnSelectMsg').on('click', function(){
		$('#layer_opt_status').val(''); 
		var url = rootPath + '/wechatMsgConfig/toSelectWechatMsgConfigPage';
		var index = showDialogUrl('选择消息', 1000, 450, url, function(index){
			var optStatus = $('#layer_opt_status').val();
			if (optStatus == '1') {
			}
			layer.close(index);
		})
		layer.full(index);
	})

	$("#btn_save").click(function() {
		$("#wechatMsgKeywordReplyForm").submit();
		return false;
	});
	
	$("#wechatMsgKeywordReplyForm").validate({
		rules : {
			keyword : {
				required : true,
				maxlength : 10
			},
			matchRule : {
				required : true,
			},
			msgId : {
				required : true,
			},
			effectiveBeginTime : {
				required : true
			},
			effectiveEndTime : {
				required : true
			},
		},
		messages : {
			keyword : {
				required : '请填写关键字',
				maxlength : '关键字至多10个字符'
			},
			matchRule : {
				required : '请选择匹配规则' 
			},
			msgId : {
				required : '请选择消息'
			},
			effectiveBeginTime : {
				required : '请填写生效开始时间' 
			},
			effectiveEndTime : {
				required : '请填写生效结束时间'
			},
		},
		errorPlacement : function(error, element) { // 错误信息位置设置方法
			var tagName = $(element).is("select");
    		if(!tagName) {
    			error.insertAfter(element);
    		}else {
    			error.insertAfter(element.parent().next());
    		}
		},
		submitHandler : function(form) {
			//异步提交表单
			$(form).ajaxSubmit({
				type		: 'post',
				success		: function(json){ 
					if (json.resultCode == '1') {
						window.location.href = rootPath + "/wechatMsgKeywordReply/toListWechatMsgKeywordReplyPage";
					} else { 
						showErr("保存失败!");
					}
				}
			}); 
		}
	});
	
	$("#btn_back").click(function() {
		window.location.href = rootPath + "/wechatMsgKeywordReply/toListWechatMsgKeywordReplyPage";
	});
	
});

